﻿
// GraphicView.cpp: CGraphicView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Graphic.h"
#endif

#include "GraphicDoc.h"
#include "GraphicView.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CGraphicView

IMPLEMENT_DYNCREATE(CGraphicView, CView)

BEGIN_MESSAGE_MAP(CGraphicView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_COMMAND(IDM_FILE_WRITE, &CGraphicView::OnFileWrite)
	ON_COMMAND(IDM_FILE_READ, &CGraphicView::OnFileRead)
	ON_COMMAND(IDM_DOT, &CGraphicView::OnDot)
	ON_COMMAND(IDM_LINE, &CGraphicView::OnLine)
	ON_COMMAND(IDM_RECTANGLE, &CGraphicView::OnRectangle)
	ON_COMMAND(IDM_ELLIPSE, &CGraphicView::OnEllipse)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

// CGraphicView 构造/析构

CGraphicView::CGraphicView() noexcept
{
	// TODO: 在此处添加构造代码
	m_nDrawType = 0;
	m_ptOrigin = 0;
}

CGraphicView::~CGraphicView()
{
}

BOOL CGraphicView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CGraphicView 绘图

void CGraphicView::OnDraw(CDC* pDC)
{
	CGraphicDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
	//int nCount = m_obArray.GetSize();
	int nCount = pDoc->m_obArray.GetSize();
	for (int i = 0; i < nCount; i++)
	{
		//((CGraph*)m_obArray.GetAt(i))->Draw(pDC);
		((CGraph*)pDoc->m_obArray.GetAt(i))->Draw(pDC);
	}
}


// CGraphicView 打印

BOOL CGraphicView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CGraphicView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CGraphicView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CGraphicView 诊断

#ifdef _DEBUG
void CGraphicView::AssertValid() const
{
	CView::AssertValid();
}

void CGraphicView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGraphicDoc* CGraphicView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicDoc)));
	return (CGraphicDoc*)m_pDocument;
}
#endif //_DEBUG


// CGraphicView 消息处理程序


void CGraphicView::OnFileWrite()
{
	// TODO: 在此添加命令处理程序代码
	CFile file("1.txt", CFile::modeCreate | CFile::modeWrite);
	CArchive ar(&file, CArchive::store); // 通过这个对象可以进行多类型的保存
	int i = 4;
	char ch = 'a';
	float f = 1.3f;
	CString str("www.baidu.com");
	ar << i << ch << f << str;
}


void CGraphicView::OnFileRead()
{
	// TODO: 在此添加命令处理程序代码
	CFile file("1.txt", CFile::modeRead);
	CArchive ar(&file, CArchive::load);
	int i;
	char ch;
	float f;
	CString str;
	CString strResult;
	ar >> i >> ch >> f >> str;
	strResult.Format("%d,%c,%f,%s", i, ch, f, str);
	MessageBox(strResult);

}


void CGraphicView::OnDot()
{
	// TODO: 在此添加命令处理程序代码
	m_nDrawType = 1;
}


void CGraphicView::OnLine()
{
	// TODO: 在此添加命令处理程序代码
	m_nDrawType = 2;

}


void CGraphicView::OnRectangle()
{
	// TODO: 在此添加命令处理程序代码
	m_nDrawType = 3;

}


void CGraphicView::OnEllipse()
{
	// TODO: 在此添加命令处理程序代码
	m_nDrawType = 4;

}


void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	m_ptOrigin = point;
	CView::OnLButtonDown(nFlags, point);
}


void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CClientDC dc(this);
	CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	CBrush* pOldBrush = dc.SelectObject(pBrush);
	switch (m_nDrawType)
	{
	case 1:
	{
		dc.SetPixel(point, RGB(0, 0, 0));
		break;
	}
	case 2:
	{
		dc.MoveTo(m_ptOrigin);
		dc.LineTo(point);
		break;
	}
	case 3:
	{
		dc.Rectangle(CRect(m_ptOrigin, point));
		break;
	}
	case 4:
	{
		dc.Ellipse(CRect(m_ptOrigin, point));
		break;
	}
	default:
		break;
	}
	CGraph* pGraph = new CGraph(m_nDrawType, m_ptOrigin, point);

	//m_obArray.Add(pGraph);

	GetDocument()->m_obArray.Add(pGraph);


	dc.SelectObject(pOldBrush);
	CView::OnLButtonUp(nFlags, point);
}
